﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using UnityEditor;
using UnityEngine;

public class RayTraceEditor:EditorWindow
{
    public static string ImagePath ="";

    [MenuItem("RayTrace/BakeRay")]
    public static void BakeRayTrace()
    {
        RayTraceEditor win = new RayTraceEditor();
        win.Show();
    }

    void OnGUI()
    {
        if (GUILayout.Button("漫反射"))
        {
            var path = Application.dataPath + "/RayTraceFile/Diffusing.png";
            if (File.Exists(path)) { File.Delete(path); }
            ImagePath = path;
            CreatePng(200, 100, CreateColorsForDiffusing(200, 100));
        }
        if (GUILayout.Button("镜面反射"))
        {
            var path = Application.dataPath + "/RayTraceFile/metalTest.png";
            if (File.Exists(path)){File.Delete(path);}
            ImagePath = path;
            CreatePng(200, 100, CreateColorForTestMetal(200, 100));
        }
        if (GUILayout.Button("镜面反射模糊"))
        {
            var path = Application.dataPath + "/RayTraceFile/metalFuzzTest.png";
            if (File.Exists(path)) { File.Delete(path); }
            ImagePath = path;
            CreatePng(200, 100, CreateColorForTestMetal(200, 100));
        }
        if (GUILayout.Button("折射模型"))
        {
            var path = Application.dataPath + "/RayTraceFile/DielectricTest.png";
            if (File.Exists(path)) { File.Delete(path); }
            ImagePath = path;
            CreatePng(256, 128, CreateColorForTestDielectric(256, 128));
        }
        if (GUILayout.Button("设置相机"))
        {
            var path = Application.dataPath + "/RayTraceFile/CameraTest.png";
            if (File.Exists(path)) { File.Delete(path); }
            ImagePath = path;
            CreatePng(256, 128, CreateColorForTestCamera(256, 128));
        }
        if (GUILayout.Button("设置DOF相机"))
        {
            var path = Application.dataPath + "/RayTraceFile/CameraDOFTest.png";
            if (File.Exists(path)) { File.Delete(path); }
            ImagePath = path;
            CreatePng(256, 128, CreateColorForTestDofCamera(256, 128));
        }
        if (GUILayout.Button("设置随机场景"))
        {
            var path = Application.dataPath + "/RayTraceFile/RondomSceneTest2_6_18_-25_fov_45_result.png";
            if (File.Exists(path)) { File.Delete(path); }
            ImagePath = path;
            CreatePng(2880, 1440, CreateColorForTestRondomScene(2880, 1440));
        }
    }

    public static Color[] CreateTestPng(int width, int height)
    {
        int l = width * height;
        Color[] colors = new Color[l];
        for (int h = height-1; h >=0; h--)
        {
            for (int w = 0; w < width; w++)
            {
                colors[w + h * width] = new Color(w / (float)width, h / (float)height, 0.2f);
            }
        }
        return colors;
    }

    public static void CreatePng(int width, int height, Color[] colors)
    {
        if (width * height != colors.Length)
        {
            Debug.LogError(string.Format("创建图标大小和颜色数组大小不符:wight:{0},height:{1},colorLenght:{2}!",width,height,colors.Length));
            return;
        }
        Texture2D tex = new Texture2D(width, height, TextureFormat.ARGB32, false);
        tex.SetPixels(colors);
        tex.Apply();
        byte[] bytes = tex.EncodeToPNG();
        FileStream fs = new FileStream(ImagePath, FileMode.CreateNew);
        BinaryWriter sw = new BinaryWriter(fs);
        sw.Write(bytes);
        sw.Close();
        fs.Close();
        AssetDatabase.Refresh();
    }

    #region 1
    public static Color GetColorForTestRay(Ray ray)
    {
        float t = 0.5f * (ray.normalDirection.y + 1f);//weight
        return (1 - t) * new Color(1, 1, 1) + t * new Color(0.5f, 0.7f, 1);//lerp
    }

    public static Color[] CreaterColorForTestRay(int width, int height)
    {
        //视锥体左下角，长宽和起始扫射点设定
        int nx = width;
        int ny = height;
        Vector3 origin = Vector3.zero;
        Vector3 lb_center = new Vector3(-2f, -1f, -1);
        Vector3 horizonal = new Vector3(4, 0, 0);
        Vector3 vertical = new Vector3(0, 2, 0);
        Color[] colors = new Color[nx * ny];
        for (int i = ny -1; i >=0 ; i--)
        {
            for (int n = 0; n < nx; n++)
            {
                Ray r = new Ray(origin, lb_center + new Vector3(n * 1.0f * horizonal.x / nx, i * 1.0f * vertical.y / ny));
                colors[n + i * nx] = CreateColorForHitSphere_Normal(r);
            }
        }
        return colors;
    }
    #endregion

    #region addSphere

    public static bool Hit_Sphere(Vector3 center,float radius , Ray r)
    {
        Vector3 oc = r.original - center;
        float a = Vector3.Dot(r.direction, r.direction);
        float b = 2.0f * Vector3.Dot(oc, r.direction);
        float c = Vector3.Dot(oc, oc) - radius * radius;
        float discriminant = b * b - 4 * a * c;
        return (discriminant > 0);
    }

    public static Color CreateColorForHitSphere(Ray r)
    {
        if (Hit_Sphere(new Vector3(0, 0, -1), 0.5f, r))
        {
            return new Color(1, 0, 0);
        }
        Vector3 unit_direction = r.normalDirection;
        float t = 0.5f * (unit_direction.y + 1f);
        return (1 - t) * new Color(1, 1, 1) + t * new Color(0.5f, 0.7f, 1);
    }
    #endregion

    #region addnormal
    public static float Hit_Sphere_Normal(Vector3 center, float radius, Ray r)
    {
        Vector3 oc = r.original - center;
        float a = Vector3.Dot(r.direction, r.direction);
        float b = 2.0f * Vector3.Dot(oc, r.direction);
        float c = Vector3.Dot(oc, oc) - radius * radius;
        float discriminant = b * b - 4 * a * c;
        if (discriminant < 0)
        {
            return -1.0f;
        }
        else {
            return (-b - Mathf.Sqrt(discriminant)) / (2.0f * a); // 点到
        }
    }

    public static Color CreateColorForHitSphere_Normal(Ray r)
    {
        var t = Hit_Sphere_Normal(new Vector3(0, 0, -1), 0.5f, r);
        if (t > 0)
        {
            var  n = r.GetRay(t).normalized - new Vector3(0, 0, -1);
            return 0.5f * new Color(n.x + 1, n.y + 1, n.z + 1);
        }
        Vector3 unit_direction = r.normalDirection;
        float lerb = 0.5f * (unit_direction.y + 1f);
        return (1 - lerb) * new Color(1, 1, 1) + lerb * new Color(0.5f, 0.7f, 1);
    }
    #endregion

    #region listobject
    public static Color GetWorldHitColor(Ray r,ref Hitable world)
    {
        HitRecord rec = new HitRecord();
        if (world.Hit(r, 0, float.MaxValue, ref rec))
        {
            return 0.5f * new Color(rec.normal.x + 1, rec.normal.y + 1, rec.normal.z + 1);
        }
        else
        {
            Vector3 direction = r.direction.normalized;
            float t = 0.5f * (direction.y +1);
            return (1f - t) * new Color(1, 1, 1) + t * new Color(0.5f, 0.7f, 1f);
        }
    }

    public static Color[] CreateColorsForListObject(int width, int height)
    {
        //视锥体左下角，长宽和起始扫射点设定
        int nx = width;
        int ny = height;
        Vector3 origin = Vector3.zero;
        Vector3 lb_center = new Vector3(-2f, -1f, -1);
        Vector3 horizonal = new Vector3(4, 0, 0);
        Vector3 vertical = new Vector3(0, 2, 0);
        Color[] colors = new Color[nx * ny];

        List<Hitable> list = new List<Hitable>();
        list.Add( new Sphere(new Vector3(0, 0, -1), 0.5f));
        list.Add( new Sphere(new Vector3(0, -100.5f, 1), 100));

        Hitable listObject = new HitableList(list);
        for (int i = ny - 1; i >= 0; i--)
        {
            for (int n = 0; n < nx; n++)
            {
                Ray r = new Ray(origin, lb_center + new Vector3(n * 1.0f * horizonal.x / nx, i * 1.0f * vertical.y / ny));
                colors[n + i * nx] = GetWorldHitColor(r,ref listObject);
            }
        }
        return colors;
    }
    #endregion

    #region antia-aliasing

    public static Color[] CreateColorsForAatialiasing(int width, int height)
    {
        //视锥体左下角，长宽和起始扫射点设定
        int nx = width;
        int ny = height;
        int ns = 100;
        Vector3 origin = Vector3.zero;
        Vector3 lb_center = new Vector3(-2f, -1f, -1);
        Vector3 horizonal = new Vector3(4, 0, 0);
        Vector3 vertical = new Vector3(0, 2, 0);
        Color[] colors = new Color[nx * ny];

        List<Hitable> list = new List<Hitable>();
        list.Add(new Sphere(new Vector3(0, 0, -1), 0.5f));
        list.Add(new Sphere(new Vector3(0, -100.5f, 1), 100));
        float angle = Mathf.Cos(90);
        Camera cam = new Camera(angle,1f*nx/ny);
        Hitable listObject = new HitableList(list);

        float perWeight = 1f / nx;
        float perHeight = 1f / ny;
        float simple_Weight = 1f / ns;
        for (int i = ny - 1; i >= 0; i--)
        {
            for (int n = 0; n < nx; n++)
            {
                int index = n + i * nx;
                Color color = new Color(0, 0, 0);
                for (int s = 0; s < ns; s++)
                {
                    // Ray r = new Ray(origin, lb_center + new Vector3(n * 1.0f * horizonal.x / nx, i * 1.0f * vertical.y / ny));
                   
                    float u = (n + UnityEngine.Random.Range(0f, 1f)) * perWeight;
                    float v = (i + UnityEngine.Random.Range(0f, 1f)) * perHeight; ;
                    Ray r = cam.GetRay(u, v);
                    color += GetWorldHitColor(r, ref listObject);
                }
                color *= simple_Weight;
                color.a = 1;
                colors[index] = color;
            }
        }
        return colors;
    }

    #endregion

    #region test diffuse

    public static Color GetColorForTestDiffusing(Ray ray, Hitable hitableList)
    {
        HitRecord rec =new HitRecord();
        if (hitableList.Hit(ray, 0.0001f, float.MaxValue, ref rec))
        {
            Vector3 target = rec.p + rec.normal + _M.GetRandomPointInUnitSphere();
            //此处假定有50 % 的光被吸收，剩下的则从入射点开始取随机方向再次发射一条射线
            return 0.5f * GetColorForTestDiffusing(new Ray(rec.p, target - rec.p), hitableList);
        }
        float t = 0.5f * (ray.normalDirection.y + 1f);
        return (1 - t) * new Color(1, 1, 1) + t * new Color(0.5f, 0.7f, 1);
    }

    public static Color[] CreateColorsForDiffusing(int width, int height)
    {
        //视锥体左下角，长宽和起始扫射点设定
        int nx = width;
        int ny = height;
        int ns = 50;
        Vector3 origin = Vector3.zero;
        Vector3 lb_center = new Vector3(-2f, -1f, -1);
        Vector3 horizonal = new Vector3(4, 0, 0);
        Vector3 vertical = new Vector3(0, 2, 0);
        Color[] buff = new Color[nx * ny];

        List<Hitable> list = new List<Hitable>();
        list.Add(new Sphere(new Vector3(0, 0, -1), 0.5f));
        list.Add(new Sphere(new Vector3(0, -100.5f, -1), 100f));
        float angle = Mathf.Cos(90);
        Camera cam = new Camera(angle, 1f * nx / ny);
        Hitable listObject = new HitableList(list);
  
        float perWeight = 1f / nx;
        float perHeight = 1f / ny;
        float simple_Weight = 1f / ns;
        for (int i = ny - 1; i >= 0; i--)
        {
            for (int n = 0; n < nx; n++)
            {
                int index = n + i * nx;
                Color color = new Color(0, 0, 0);
                for (int s = 0; s < ns; s++)
                {
                    float u = (n + UnityEngine.Random.Range(0f, 1f)) * perWeight;
                    float v = (i + UnityEngine.Random.Range(0f, 1f)) * perHeight; ;
                    Ray r = cam.GetRay(u, v);
                    color += GetColorForTestDiffusing(r, listObject);
                    //color += GetWorldHitColor(r,ref listObject);
                }
                color *= simple_Weight;
                //gamma值 ，gamma函数就是物理光强和主观灰度之间的映射关系
                //color = new Color(Mathf.Sqrt(color.r), Mathf.Sqrt(color.g), Mathf.Sqrt(color.b), 1f);
                color.a = 1;
                buff[index] = color;
            }
        }
        return buff;
    }
    #endregion

    #region metal
    static int Max_Scatter_Time= 50;
    public static Color GetColorForTestMaterial(Ray ray, Hitable hitableList,int depth)
    {
        HitRecord rec = new HitRecord();
        if (hitableList.Hit(ray, 0.0001f, float.MaxValue, ref rec))
        {
            Ray r = new Ray(Vector3.zero, Vector3.zero);
            Color attenuation = Color.black;
            if (depth < Max_Scatter_Time && rec.material.scatter(ray, rec, ref attenuation, ref r))
            {
                Color c = GetColorForTestMaterial(r, hitableList, depth + 1);
                return new Color(c.r * attenuation.r, c.g * attenuation.g, c.b * attenuation.b);
            }
            else
            {
                //假设已经反射了太多次，或者压根就没有发生反射，那么就认为黑了
                return Color.black;
            }
        }
        float t = 0.5f * (ray.normalDirection.y + 1f);
        return (1 - t) * new Color(1, 1, 1) + t * new Color(0.5f, 0.7f, 1);
    }

    public static Color[] CreateColorForTestMetal(int width, int height)
    {
        //视锥体左下角，长宽和起始扫射点设定
        int nx = width;
        int ny = height;
        int ns = 50;
        Vector3 origin = Vector3.zero;
        Vector3 lb_center = new Vector3(-2f, -1f, -1);
        Vector3 horizonal = new Vector3(4, 0, 0);
        Vector3 vertical = new Vector3(0, 2, 0);
       
        List<Hitable> list = new List<Hitable>();
        list.Add(new Sphere(new Vector3(0, 0, -1), 0.5f, new Lambertian(new Color(0.8f, 0.3f, 0.3f))));
        list.Add(new Sphere(new Vector3(0, -100.5f, -1), 100f, new Lambertian(new Color(0.8f, 0.8f, 0.0f))));
        list.Add(new Sphere(new Vector3(1, 0, -1), 0.5f, new Metal(new Color(0.8f, 0.6f, 0.2f))));
        list.Add(new Sphere(new Vector3(-1, 0, -1), 0.5f, new Metal(new Color(0.8f, 0.8f, 0.8f))));
        Color[] colors = new Color[ny*nx];
        float angle = Mathf.Cos(90);
        Camera cam = new Camera(angle, 1f * nx / ny);
        Hitable listObject = new HitableList(list);

        float perWeight = 1f / nx;
        float perHeight = 1f / ny;
        float simple_Weight = 1f / ns;
        for (int i = ny - 1; i >= 0; i--)
        {
            for (int n = 0; n < nx; n++)
            {
                int index = n + i * nx;
                Color color = new Color(0, 0, 0);
                for (int s = 0; s < ns; s++)
                {
                    float u = (n + UnityEngine.Random.Range(0f, 1f)) * perWeight;
                    float v = (i + UnityEngine.Random.Range(0f, 1f)) * perHeight; ;
                    Ray r = cam.GetRay(u, v);
                    color += GetColorForTestMaterial(r, listObject,0);
                }
                color *= simple_Weight;
                //gamma值 ，gamma函数就是物理光强和主观灰度之间的映射关系
                color = new Color(Mathf.Sqrt(color.r), Mathf.Sqrt(color.g), Mathf.Sqrt(color.b), 1f);
                color.a = 1;
                colors[index] = color;
            }
        }
        return colors;
    }

    #endregion

    #region dielectric
    public static Color[] CreateColorForTestDielectric(int width, int height)
    {
        //视锥体左下角，长宽和起始扫射点设定
        int nx = width;
        int ny = height;
        int ns = 50;
        Vector3 origin = Vector3.zero;
        Vector3 lb_center = new Vector3(-2f, -1f, -1);
        Vector3 horizonal = new Vector3(4, 0, 0);
        Vector3 vertical = new Vector3(0, 2, 0);

        List<Hitable> list = new List<Hitable>();
        list.Add(new Sphere(new Vector3(0, 0, -1), 0.5f, new Lambertian(new Color(0.1f, 0.2f, 0.5f))));
        list.Add(new Sphere(new Vector3(0, -100.5f, -1), 100f, new Lambertian(new Color(0.8f, 0.8f, 0.0f))));
        list.Add(new Sphere(new Vector3(1, 0, -1), 0.5f, new Metal(new Color(0.8f, 0.6f, 0.2f))));
        list.Add(new Sphere(new Vector3(-1, 0, -1), 0.45f, new Dielectrics(1.5f)));

        Color[] colors = new Color[ny * nx];    
        Camera cam = new Camera(90, 1f * nx / ny);
        Hitable listObject = new HitableList(list);

        float perWeight = 1f / nx;
        float perHeight = 1f / ny;
        float simple_Weight = 1f / ns;
        for (int i = ny - 1; i >= 0; i--)
        {
            for (int n = 0; n < nx; n++)
            {
                int index = n + i * nx;
                Color color = new Color(0, 0, 0);
                for (int s = 0; s < ns; s++)
                {
                    float u = (n + UnityEngine.Random.Range(0f, 1f)) * perWeight;
                    float v = (i + UnityEngine.Random.Range(0f, 1f)) * perHeight; ;
                    Ray r = cam.GetRay(u, v);
                    color += GetColorForTestMaterial(r, listObject, 0);
                }
                color *= simple_Weight;
                //gamma值 ，gamma函数就是物理光强和主观灰度之间的映射关系
                color = new Color(Mathf.Sqrt(color.r), Mathf.Sqrt(color.g), Mathf.Sqrt(color.b), 1f);
                color.a = 1;
                colors[index] = color;
            }
        }
        return colors;
    }

    #endregion

    #region set Camera
    public static Color[] CreateColorForTestCamera(int width, int height)
    {
        //视锥体左下角，长宽和起始扫射点设定
        int nx = width;
        int ny = height;
        int ns = 50;
        Vector3 origin = Vector3.zero;
        Vector3 lb_center = new Vector3(-2f, -1f, -1);
        Vector3 horizonal = new Vector3(4, 0, 0);
        Vector3 vertical = new Vector3(0, 2, 0);

        List<Hitable> list = new List<Hitable>();
        list.Add(new Sphere(new Vector3(0, 0, -1), 0.5f, new Lambertian(new Color(0.1f, 0.2f, 0.5f))));
        list.Add(new Sphere(new Vector3(0, -100.5f, -1), 100f, new Lambertian(new Color(0.8f, 0.8f, 0.0f))));
        list.Add(new Sphere(new Vector3(1, 0, -1), 0.5f, new Metal(new Color(0.8f, 0.6f, 0.2f))));
        list.Add(new Sphere(new Vector3(-1, 0, -1), 0.5f, new Dielectrics(1.5f)));
        //list.Add(new Sphere(new Vector3(-1, 0, -1), -0.45f, new Dielectrics(1.5f)));

        Color[] colors = new Color[ny * nx];

        var lookFrom = new Vector3(-2, 2, 1);
        var lookTo = new Vector3(0, 0, -1);
        float fov = 90;
        float dist_to_focus = (lookFrom - lookTo).magnitude;
        Camera cam = new Camera(lookFrom,lookTo,new Vector3(0,1,0), fov, 1f * nx / ny);
        Hitable listObject = new HitableList(list);

        float perWeight = 1f / nx;
        float perHeight = 1f / ny;
        float simple_Weight = 1f / ns;
        for (int i = ny - 1; i >= 0; i--)
        {
            for (int n = 0; n < nx; n++)
            {
                int index = n + i * nx;
                Color color = new Color(0, 0, 0);
                for (int s = 0; s < ns; s++)
                {
                    float u = (n + UnityEngine.Random.Range(0f, 1f)) * perWeight;
                    float v = (i + UnityEngine.Random.Range(0f, 1f)) * perHeight; ;
                    Ray r = cam.GetRay(u, v);
                    color += GetColorForTestMaterial(r, listObject, 0);
                }
                color *= simple_Weight;
                //gamma值 ，gamma函数就是物理光强和主观灰度之间的映射关系
                color = new Color(Mathf.Sqrt(color.r), Mathf.Sqrt(color.g), Mathf.Sqrt(color.b), 1f);
                color.a = 1;
                colors[index] = color;
            }
        }
        return colors;
    }

    #endregion

    #region setDof

    public static Color[] CreateColorForTestDofCamera(int width, int height)
    {
        //视锥体左下角，长宽和起始扫射点设定
        int nx = width;
        int ny = height;
        int ns = 50;
        Vector3 origin = Vector3.zero;
        Vector3 lb_center = new Vector3(-2f, -1f, -1);
        Vector3 horizonal = new Vector3(4, 0, 0);
        Vector3 vertical = new Vector3(0, 2, 0);

        List<Hitable> list = new List<Hitable>();
        list.Add(new Sphere(new Vector3(0, 0, -1), 0.5f, new Lambertian(new Color(0.1f, 0.2f, 0.5f))));
        list.Add(new Sphere(new Vector3(0, -100.5f, -1), 100f, new Lambertian(new Color(0.8f, 0.8f, 0.0f))));
        list.Add(new Sphere(new Vector3(1, 0, -1), 0.5f, new Metal(new Color(0.8f, 0.6f, 0.2f))));
        list.Add(new Sphere(new Vector3(-1, 0, -1), 0.5f, new Dielectrics(1.5f)));
        //list.Add(new Sphere(new Vector3(-1, 0, -1), -0.45f, new Dielectrics(1.5f)));

        Color[] colors = new Color[ny * nx];

        var lookFrom = new Vector3(3, 3,2);
        var lookTo = new Vector3(0, 0, -1);
        float fov = 20;
        float aperture = 2f;
        float dist_to_focus = (lookFrom - lookTo).magnitude;
        Camera cam = new Camera(lookFrom, lookTo, new Vector3(0, 1, 0), fov, 1f * nx / ny, aperture, dist_to_focus);
        Hitable listObject = new HitableList(list);

        float perWeight = 1f / nx;
        float perHeight = 1f / ny;
        float simple_Weight = 1f / ns;
        for (int i = ny - 1; i >= 0; i--)
        {
            for (int n = 0; n < nx; n++)
            {
                int index = n + i * nx;
                Color color = new Color(0, 0, 0);
                for (int s = 0; s < ns; s++)
                {
                    float u = (n + UnityEngine.Random.Range(0f, 1f)) * perWeight;
                    float v = (i + UnityEngine.Random.Range(0f, 1f)) * perHeight; ;
                    Ray r = cam.GetRay(u, v);
                    color += GetColorForTestMaterial(r, listObject, 0);
                }
                color *= simple_Weight;
                //gamma值 ，gamma函数就是物理光强和主观灰度之间的映射关系
                color = new Color(Mathf.Sqrt(color.r), Mathf.Sqrt(color.g), Mathf.Sqrt(color.b), 1f);
                color.a = 1;
                colors[index] = color;
            }
        }
        return colors;
    }
    #endregion

    #region randomScene
    public static Color[] CreateColorForTestRondomScene(int width, int height)
    {
        //视锥体左下角，长宽和起始扫射点设定
        int nx = width;
        int ny = height;
        int ns = 100;
        Vector3 origin = Vector3.zero;
        Vector3 lb_center = new Vector3(-2f, -1f, -1);
        Vector3 horizonal = new Vector3(4, 0, 0);
        Vector3 vertical = new Vector3(0, 2, 0);

        Color[] colors = new Color[ny * nx];

        var lookFrom = new Vector3(6, 1.8f, -2.5f);
        var lookTo = new Vector3(0, 0, -1);
        float fov = 45;
        float aperture =0;
        float dist_to_focus = 1;/* (lookFrom - lookTo).magnitude*/;
        Camera cam = new Camera(lookFrom, lookTo, new Vector3(0, 1, 0), fov, 1f * nx / ny, aperture, dist_to_focus);
        Hitable listObject = _M.GetRandom_Scene();

        float perWeight = 1f / nx;
        float perHeight = 1f / ny;
        float simple_Weight = 1f / ns;
        for (int i = ny - 1; i >= 0; i--)
        {
            for (int n = 0; n < nx; n++)
            {
                int index = n + i * nx;
                Color color = new Color(0, 0, 0);
                for (int s = 0; s < ns; s++)
                {
                    float u = (n + UnityEngine.Random.Range(0f, 1f)) * perWeight;
                    float v = (i + UnityEngine.Random.Range(0f, 1f)) * perHeight; ;
                    Ray r = cam.GetRay(u, v);
                    color += GetColorForTestMaterial(r, listObject, 0);
                }
                color *= simple_Weight;
                //gamma值 ，gamma函数就是物理光强和主观灰度之间的映射关系
                color = new Color(Mathf.Sqrt(color.r), Mathf.Sqrt(color.g), Mathf.Sqrt(color.b), 1f);
                color.a = 1;
                colors[index] = color;
            }
        }
        return colors;
    }
    #endregion
}
